﻿using Digitalmes.DataAccess;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;

namespace Digitalmes.Services.DbFactory
{
    public class DbContextFactory : IDbContextFactory,ISingleton
    {
        private readonly IConfiguration _configuration;
        private readonly Dictionary<string, CS> _connections;
        public DbContextFactory(IConfiguration configuration)
        {
            _configuration = configuration;
            _connections = _configuration.GetSection("ConnectionDB").Get<List<CS>>().ToDictionary(c => c.Key);
        }
        public EmptyContext CreateDbContext(string connectionName)
        {
            if (_connections.TryGetValue(connectionName, out var cs))
            {
                if (connectionName == "FdsWeber")
                    return new FdsWeberDbContext(cs);
            }
            throw new Exception($"Connection string '{connectionName}' not found.");
        }
    }
}
